/*
 * @(#)Xfsw.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsw;

import xfuzzy.lang.*;
import java.io.*;

/**
 * Herramienta que desarrolla la s�ntesis software a C, C++ o Java
 * 
 * @author Francisco Jos� Moreno Velo 
 *
 */
public class Xfsw {

	//----------------------------------------------------------------------------//
	//                            COSTANTES P�BLICAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo para indicar el lenguaje C estandar
	 */
	public static int ANSI_C = 0;
	
	/**
	 * C�digo para indicar el lenguaje C++
	 */
	public static int C_PLUS_PLUS = 1;
	
	/**
	 * C�digo para indicar el lenguaje Java
	 */
	public static int JAVA = 2;
	
	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * Car�cter de fin de linea dependiente de la plataforma
	 */
	private static String eol = System.getProperty("line.separator", "\n");

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Mensaje a mostrar al terminar la ejecuci�n
	 */
	private String msg;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public Xfsw(Specification spec, File dir, int lang) {
		this.msg = "";
		File wdir = null; 
		if(dir == null) wdir = spec.getFile().getParentFile();
		else wdir = dir;
		
		if(lang == ANSI_C) this.msg = generateC(spec,wdir);
	}

	//----------------------------------------------------------------------------//
	//                        M�TODOS P�BLICOS EST�TICOS                          //
	//----------------------------------------------------------------------------//

	/**
	 * Ejecuci�n externa
	 */
	public static void main(String args[]) {
		String filename = "";
		int lang = ANSI_C;
		if(args.length == 1) filename = args[0];
		else if(args.length == 2) {
			if(args[0].equals("-ansic")) lang = ANSI_C;
			if(args[0].equals("-c++")) lang = C_PLUS_PLUS;
			if(args[0].equals("-java")) lang = JAVA;
			filename = args[1];
		}
		else {
			System.out.println("Usage: xfsw (-ansic | -c++ | -java) xflfile");
			System.exit(-1);
		}
		XflParser xflparser = new XflParser();
		Specification spec = xflparser.parse(filename);
		if(spec == null) { System.out.println(xflparser.resume()); System.exit(-1); }
		Xfsw compiler = new Xfsw(spec, null, lang);
		System.out.println(compiler.getMessage());
		System.exit(0);
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * M�todo que describe el resultado
	 */
	public String getMessage() {
		return this.msg;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Genera la descripci�n en ANSI-C
	 */
	private String generateC(Specification spec, File wdir) {
		String msg = "";
		try {
			String msgh = XfswCHeader.create(spec,wdir);
			String msgc = XfswCCode.create(spec,wdir);
			msg += "C code generation describing the fuzzy system "+spec.getName();
			msg += " has been successfully finished."+eol;
			msg += "C description is included in files: "+eol;
			msg += "        \""+msgh+"\","+eol;
			msg += "        and \""+msgc+"\".";
		} catch(XflException ex) {
			msg += "Fatal error generating a C description.";
		}
		return msg;
	}
}
